package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.CommentResult;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.volley.GetBitmap;

import java.util.List;

/**
 * Created by dllo on 16/1/27.
 */
public class CommentAdapter extends BaseAdapter {
    private Context context;
    private List<CommentResult.ListEntity> list;

    public CommentAdapter(Context context) {
        this.context = context;
    }

    public void addListData(List<CommentResult.ListEntity> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null && list.size() > 0 ?
                list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentViewHolder commentHolder = null;
        if (list != null && list.size() > 0) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context)
                        .inflate(R.layout.fragment_comment_adapter_item,parent,false);
                commentHolder = new CommentViewHolder(convertView);
                convertView.setTag(commentHolder);
            }
            else {
                commentHolder = (CommentViewHolder) convertView.getTag();
            }
            Log.d("CommentAdapter", list.get(position).getNickname());
            commentHolder.nameTv.setText(list.get(position).getNickname());
            commentHolder.commentTv.setText(list.get(position).getContent());
            commentHolder.likeTv.setText(list.get(position).getLikes()+"");
            GetBitmap.getBitmap(list.get(position).getSmallHeader(),commentHolder.circleIv);
        }
        return convertView;
    }

    class CommentViewHolder {
        private Circlelmage circleIv;
        private TextView nameTv,commentTv,likeTv;

        public CommentViewHolder(View view) {
            circleIv = (Circlelmage) view.findViewById(R.id.iv_commentAdapter);
            nameTv = (TextView) view.findViewById(R.id.tvName_commentAdapter);
            commentTv = (TextView) view.findViewById(R.id.tvComment_commentAdapter);
            likeTv = (TextView) view.findViewById(R.id.tvLike_commentAdapter);
        }
    }
}
